<?php
/*
**2015-06-26
**天时同城对接
**BY user pzp
*/
class TianShiTongCheng{	
	// private $tstc_url ='http://ndh.sjdzp.com/Api/Seller/api.json';
	public function __construct($tid,$aid){
		
		$sel = "select supplierIdentity,signkey from uu_qunar_use where DockingMode=5 and (tid=$tid or fid=$aid) limit 1";
		$GLOBALS['le']->query($sel);
		$GLOBALS['le']->fetch_assoc();		
		$supplierIdentity = explode("|",$GLOBALS['le']->f('supplierIdentity'));  
		$this->_pid = $supplierIdentity[0];
		$this->authcode = $GLOBALS['le']->f('signkey');
		$this->tstc_url = $supplierIdentity[1];
		
	}
	 
	//门票详情
	public function Get_Ticket(){
		$request = array(
			'method'=>'item_list',
			'format'=>'xml',
			'_pid'=>$this->_pid,
			'page'=>1,
			'size'=>10,
			'cate_id'=>'',
			'zone'=>'',
			'item_id'=>'',
			'key_word'=>''			
		);
		$post_data = $this->ksort_sign($request);
		$request_data = $this->tstc_post($post_data);
		$request_data_xml = simplexml_load_string($request_data);
		$reponse_message = (string)$request_data_xml->message;
		if($reponse_message == '成功'){
			return $request_data_xml->list;
		}else{
			$this->logfile($reponse_message);
		}
	}
	
	//提交订单
	public function Submit_Order($request_array,$PFT_infunction=NULL){
		$request = array(
			'method'	=>'item_orders',
			'format'	=>'xml',
			'_pid'		=> $this->_pid,
			'is_pay'	=> $request_array['ispay'], //是否支付
			'orders_id'	=> $request_array['pftordernum'], //第三方id
			'item_id'	=> $request_array['uuid'], //门票id
			'size'		=> $request_array['UUtnum'],  //购买数量
			'name'		=> $request_array['pftordername'], //购买人姓名
			'mobile'	=> $request_array['UUordertel'], //购买人手机号
			'start_date'=> $request_array['UUplaytime'], //游玩日期
			'start_date_auto'=> 0,//是否设置自动游玩日期
			'price_type'=> 1,   //价格类型
			'remark'	=> '',//订单备注
			'price'		=> '',//产品单价
			'back_cash'	=> '',//返现金额
			'id_number'	=> $request_array['IDcar'], //身份证
		);
		if($request['id_number'] == 0) unset($request['id_number']);
		$post_data = $this->ksort_sign($request);
		$request_data = $this->tstc_post($post_data);
		$request_data_xml = simplexml_load_string($request_data);
		$reponse_message = (string)$request_data_xml->message;
		if($reponse_message == '成功'){
			$reponse_data = array(
				'tstc_orderid'=>(string)$request_data_xml->info->id,
				'tstc_ordercode'=>(string)$request_data_xml->info->code,
			);
			$PFT_infunction->logfile("10|".$request_array['pftordernum']."|".(string)$request_data_xml->info->id."|".(string)$request_data_xml->info->code,'submitO_s');
			return $reponse_data;
		}else{
			$PFT_infunction->logfile("10|".$request_array['pftordernum']."|".$request_array['UUordertel']."|".$reponse_message,'submitO_f');
			return 101;
		}
	}
	
	
	//查询订单
	public function Select_Order(){
		$request = array(
			'method'=>'orders_query',
			'format'=>'xml',
			'_pid'=>$this->_pid,
			'code'=>'2651000510',			
		);
		$post_data = $this->ksort_sign($request);
		$request_data = $this->tstc_post($post_data);
		$request_data_xml = simplexml_load_string($request_data);
		print_r($request_data_xml);
	}
	
	//修改取消订单
	public function Modify_Order($size,$UUremotenum,$tempOrder,$pftOrder,$PFT_infunction=NULL){
		$request = array(
			'method'=>'item_refund',
			'format'=>'xml',
			'_pid'=>$this->_pid,
			'orders_id'=>$UUremotenum,
			'size'=>$size,			
		);
		$post_data = $this->ksort_sign($request);
		$request_data = $this->tstc_post($post_data);
		$request_data_xml = simplexml_load_string($request_data);
		$reponse_message = (string)$request_data_xml->message;
		if($reponse_message == '成功'){
			$PFT_infunction->logfile("10|".$pftOrder."|".$tempOrder."|".$UUremotenum."|".$reponse_message,'modify_s');
			return 100;
		}else{
			$PFT_infunction->logfile("10|".$pftOrder."|".$tempOrder."|".$UUremotenum."|".$reponse_message,'modify_f');
			return 101;
		}
	}
	
	//拼接数据及加密
	public function ksort_sign($array){
		ksort($array);
		$o=""; 
		foreach ($array as $k=>$v){ 
			$o .= "$k=".$v."&";
		} 
		$post_data = substr($o,0,-1);
		$_sig = strtoupper(md5($this->authcode."&".$post_data."&".$this->authcode));
		$post_data = $post_data.'&_sig='.$_sig;
		return $post_data;
	}
	
	
	//提交数据
	public function tstc_post($post_data){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL,$this->tstc_url); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
		$reponse = curl_exec($ch);
		curl_close($ch);
		return $reponse;
	}
	
}
// $tstc = new TianShiTongCheng(9056);
// $Get_Ticket = $tstc->Get_Ticket(); print_r($Get_Ticket);
// $Submit_Order  = $tstc->Submit_Order();
// $Select_Order  = $tstc->Select_Order();
// $Modify_Order  = $tstc->Modify_Order();print_r($Modify_Order);
?>